﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using Danhmuc;
namespace libThongTin
{
    public partial class frmLinhKien : BasicForm.parent2
    {
        ThietBiInfo ObjThietBi = new ThietBiInfo();
        CtrThietBi Ctr = new CtrThietBi();

        public frmLinhKien()
        {
            CreateWaitDialog();
            InitializeComponent();
            SetWaitDialogCaption("Đang tải dữ liệu");
            InitGrid();
            Init();
            cbxPLoai.Properties.DataSource = (new CtrPhanLoai()).GetAll();
            cbxNhaCC.Properties.DataSource = (new CtrNhaCC()).GetAll();
        }        
       
        enum ActionMode
        {
            NEW =0,
            EDIT=1,
            DEL=2
        }
        ActionMode AcMode = ActionMode.NEW;

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ClearForm();
            txtMaTB.Properties.ReadOnly = false;
            txtMaTB.Focus();
            AcMode = ActionMode.NEW;
        }

        public void ClearForm()
        {
            txtMaTB.Properties.ReadOnly = false;
            txtMaTB.Text = string.Empty;
            txtTenTB.Text = string.Empty;
            cbxPLoai.Text = string.Empty;
            dtpNgaySX.Text = string.Empty;
            cbxNhaCC.Text = string.Empty;
            dtpNgayMua.Text = string.Empty;
            dtpNgaySDung.Text = string.Empty;
            txtGia.Text = string.Empty;
            txtBHanh.Text = string.Empty;
            txtThongTin.Text = string.Empty;
            txtMaTB.Focus();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ObjThietBi.MA_TB = txtMaTB.Text;
            ObjThietBi.TEN_TB = txtTenTB.Text;
            ObjThietBi.MA_PLOAI = cbxPLoai.EditValue.ToString();
            if (dtpNgaySX.Text == null || dtpNgaySX.Text == string.Empty)
            {
                ObjThietBi.NGAY_SX = DateTime.Now;
            }
            else
            {
                ObjThietBi.NGAY_SX = DateTime.Parse(dtpNgaySX.Text);
            }
            ObjThietBi.MA_NHACC = cbxNhaCC.EditValue.ToString();

            if (dtpNgayMua.Text == null || dtpNgayMua.Text == string.Empty)
            {
                ObjThietBi.NGAY_MUA = DateTime.Now;
            }
            else
            {
                ObjThietBi.NGAY_MUA = DateTime.Parse(dtpNgayMua.Text);
            }

            if (dtpNgaySDung.Text == null || dtpNgayMua.Text == string.Empty)
            {
                ObjThietBi.NGAY_SDUNG = DateTime.Now;
            }
            else
            {
                ObjThietBi.NGAY_SDUNG = DateTime.Parse(dtpNgaySDung.Text);
            }
            ObjThietBi.NGUYEN_GIA = int.Parse(txtGia.Text);
            ObjThietBi.THOIGIAN_BHANH = int.Parse(txtBHanh.Text);
            ObjThietBi.THONGTIN_TB = txtThongTin.Text;
            ObjThietBi.MA_LIENKET = null;
            ObjThietBi.IS_LKIEN = true;
            if (!CheckDate())
            {
                return;
            }
            if (AcMode == ActionMode.NEW) //them mới
            {
                try
                {
                    Ctr.InsertItem(ObjThietBi);
                    this.gridControl1.DataSource = Ctr.GetAll();
                    if (MessageBox.Show("Bạn muốn thêm tiếp không?", "Tiếp tục", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        ClearForm();
                        ObjThietBi = new ThietBiInfo();
                    }
                    else
                    {
                        txtMaTB.Properties.ReadOnly = true;
                        AcMode = ActionMode.EDIT;
                    }
                }
                catch (Exception ex)
                {
                    txtMaTB.Focus();
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            else
            {
                if (AcMode == ActionMode.EDIT) //chỉnh sửa
                {
                    try
                    {
                        Ctr.UpdateItem(ObjThietBi);
                        this.gridControl1.DataSource = Ctr.GetAll();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
                else //xóa
                {
                    try
                    {
                        Ctr.DeleteItem(ObjThietBi);
                        this.gridControl1.DataSource = Ctr.GetAll();
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        public void Init()
        {
            //kiểm tra nếu là textbox thì cho phép di chuyển tới control ke tiep bang phim enter.
            foreach (Control item in panelControl1.Controls)
            {
                if (item.GetType() == typeof(TextEdit))
                {
                    ((TextEdit)item).EnterMoveNextControl = true;
                }
            }
            txtMaTB.Focus();
            txtMaTB.Properties.CharacterCasing = CharacterCasing.Upper;
        }

        public void InitGrid()
        {
            // gridView1
            // 
            GridColumn[] Cols = new GridColumn[10];
            // 
            // gridColumn1
            // 
            int i = 0;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Mã Thiết Bị";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 0;
            Cols[i].FieldName = "MA_TB";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Caption = "Tên Thiết Bị";
            Cols[i].Width = 200;
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "TEN_TB";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Phân Loại";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "MA_PLOAI";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Ngày Sản Xuất";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGAY_SX";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Nhà Cung Cấp";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "MA_NHACC";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Ngày Mua";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGAY_MUA";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Ngày Sử Dụng";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGAY_SDUNG";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Nguyên Giá";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGUYEN_GIA";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Thời Gian Bảo Hành";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "THOIGIAN_BHANH";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Thông Tin Thiết Bị";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "THONGTIN_TB";

            //GridColumn gridColumn2 = new GridColumn();
            this.gridView1.Columns.AddRange(Cols);
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.Name = "gridView1";
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.IndicatorWidth = 30;
            //this.gridView1.OptionsSelection.
            //this.gridView1.
            this.gridControl1.DataSource = Ctr.GetAll();
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            ThietBiInfo ObjThietBi = (ThietBiInfo)gridView1.GetRow(e.RowHandle);
            //ObjNhacc = Ctr.GetByID(SelectRow["MA_NHACC"].ToString());

            //fill du lieu len form
            txtMaTB.Text = ObjThietBi.MA_NHACC;
            //txtMaTB.Properties.ReadOnly = true;
            //txtTenNhaCC.Text = ObjThietBi.TEN_NHACC;
            //txtDiaChi.Text = ObjThietBi.DIA_CHI;
            //txtNguoiLHe.Text = ObjThietBi.NGUOI_LHE;
            //txtSDT.Text = ObjThietBi.SO_DT;
            AcMode = ActionMode.EDIT;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ThietBiInfo ObjThietBi = (ThietBiInfo)gridView1.GetRow(e.FocusedRowHandle);
            //ObjNhacc = Ctr.GetByID(SelectRow["MA_NHACC"].ToString());

            //fill du lieu len form
            txtMaTB.Text = ObjThietBi.MA_TB;
            txtMaTB.Properties.ReadOnly = true;
            txtTenTB.Text = ObjThietBi.TEN_TB;
            cbxPLoai.EditValue = ObjThietBi.MA_PLOAI;
            dtpNgaySX.Text = ObjThietBi.NGAY_SX.ToString();
            cbxNhaCC.EditValue = ObjThietBi.MA_NHACC;
            txtThongTin.Text = ObjThietBi.THONGTIN_TB;
            dtpNgayMua.Text = ObjThietBi.NGAY_MUA.ToString();
            dtpNgaySDung.Text = ObjThietBi.NGAY_SDUNG.ToString();
            txtGia.Text = ObjThietBi.NGUYEN_GIA.ToString();
            txtBHanh.Text = ObjThietBi.THOIGIAN_BHANH.ToString();
            AcMode = ActionMode.EDIT;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            txtMaTB.Properties.ReadOnly = true;
            txtTenTB.Focus();
            AcMode = ActionMode.EDIT;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa không?", "Tiếp tục", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                AcMode = ActionMode.DEL;
                simpleButton4.PerformClick();
            }
        }

        private void txtBHANH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) //nhấn enter ở contrl nhập liệu cuối cùng
            {
                simpleButton4.PerformClick();
            }
        }

        public bool CheckDate()
        {
            if ((dtpNgaySX.DateTime > dtpNgayMua.DateTime) || (dtpNgaySX.DateTime > dtpNgaySDung.DateTime))
            {
                MessageBox.Show("Ngày sản xuất lớn hơn ngày mua hoặc ngày sử dụng.");
                dtpNgaySX.Text = string.Empty;
                return false;
                
            }
            if ((dtpNgayMua.DateTime > dtpNgaySDung.DateTime))
            {
                MessageBox.Show("Ngày mua lớn hơn ngày sử dụng.");
                dtpNgayMua.Text = string.Empty;
                return false;
            }
            return true;
        }

    }
}
